package prototype.deep;

import lombok.Data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Data
public class Student implements Cloneable{

    private String name;

    private Integer classid;

    private List<String> course = new ArrayList<>();

    public Student(String name, Integer classid) throws InterruptedException {
        this.name = name;
        this.classid = classid;
        // 模拟业务逻辑操作消耗10ms
        Thread.sleep(10);
        course.add("语文");
        course.add("数学");
        course.add("英语");
    }

    @Override
    public Object clone() throws CloneNotSupportedException {

        Object object = super.clone();
        Student student = (Student)object;

        //拷贝引用类型
        List<String> newCourse = new ArrayList<>();
        Iterator<String> it = student.course.iterator();
        while (it.hasNext()) {
            newCourse.add(it.next());
        }
        student.course  = newCourse;

        return object;
    }
}
